<?php

// +----------------------------------------------------------------------
// | 用户权限管理
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\ucenter\admin;

use app\controller\admin\Common;
use app\model\ucenter\UserAuthentication as UserAuthenticationModel;
use app\event\ucenter\UserAuthentication as UserAuthenticationEvent;
use app\model\ucenter\User as UserModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;

class UserAuthentication extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
        $map = [];
        if ($this->request->has('status') && !$this->request->has('field.status')) {
            $map[] = ['status', '=', $this->request->param('status')];
        } elseif (!$this->request->has('field.status')) {
            $map[] = ['status', '=', 0];
        } else {
//                $map[] = ['status', '=', 0];
        }
        return $map;
    }

    public function index()
    {
        $auths = UserAuthenticationModel::where('user_id',null)->select();

        foreach ($auths as $auth) {
            $auth['user_id'] = $auth['uid'];
            $auth->save();
        }

        // 显示页面
        $statusOptions = [
            [
                'id' => -1,
                'value' => '已拒绝',
            ],
            [
                'id' => 0,
                'value' => '待审核',
            ],
            [
                'id' => 1,
                'value' => '已经通过',
            ]
        ];
        // ID	姓名		手机号	用户姓名	身份证号	身份证正面照	身份证背面照	状态	拒绝原因	认证时间	操作时间	操作
        //ID	昵称		手机号	用户姓名	身份证号	身份证正面照	身份证背面照	状态	拒绝原因	认证时间	操作时间	操作
        $builder = new Table();
        $builder->model(UserAuthenticationModel::class)
            ->where($this->_filter())
            ->searchSelect('status', '审核状态：', $statusOptions)
            ->order('id DESC')
//				->suggest('自动审核未通过用户可以提交待审核')
            ->searchText('user_id', '用户ID', '需要精确查询')
            ->keyId()
            ->keyText('user.username', '昵称')
            ->keyText('user.username', '手机号')
            ->keyText('name', '用户姓名')
            ->keyText('idNumber', '身份证号')
            ->keyImage('fort_image', '正面照')
            ->keyImage('back_image', '背面照')
            ->keyMap('status', '状态',['未审核','已审核','已驳回'])
            ->keyText('reason', '拒绝原因')
            ->keyText('update_time', '认证时间')
            ->actionToCheck('check?id={$id}','通过认证')
//            ->keyText('create_time', '提交时间')
//            ->actionUpdate('update?id={$id}', '审核', [0])
        ;
        return $builder->fetch();
    }

    public function check()
    {
        $auth = UserAuthenticationModel::where('status',0)->find();
        if (!$auth)
        {
            $this->_returnError('信息不存在');
        }else{

            $user = UserModel::where('id',$auth['user_id'])->find();
            if (!$user)
            {
                $this->_returnError('信息不存在');
            }else{
                $auth['status'] = 1;
                $auth->save();
                $user['idCard'] = $auth['idcard_no'];
                $user['idCardName'] = $auth['name'];
                $user['nickname'] = $auth['name'];
                $user['realName'] = 1;
                $user['birthday'] = get_idcard_birthday($auth['idcard_no']);

                $user->save();
                $this->_returnSuccess('认证成功');
            }
        }

    }

    public function update()
    {
        if ($this->request->isPost()) {
            $event = new UserAuthEvent();
            $result = $event->examine($this->request->post(), $this->user);
            if (0 == $result['code']) {
                $this->_returnSuccess($result['message'], true);
            } else {
                $this->_returnError($result['message']);
            }
        } else {
            $auth = UserAuthModel::where('id', $this->request->param('id'))->find();
            if (!$auth) {
                $this->_returnError('提交信息不存在');
            }
//                $user = UserModel::where('id', $auth['user_id'])->find();
//				$user= $user->toArray();
//                $user['idcard_name'] = $auth['idcard_name'];
//				dump($auth->toArray());
            $auth['nickname'] = $auth->user['nickname'];
            $auth['email'] = $auth->user['email'];
            $builder = new Edit();
            $builder->title('审核认证用户');
            $builder->keyHidden('auth.id')
                ->keyLabel('nickname', '用户昵称', '用户昵称')
                ->keyLabel('email', '邮箱', '')
                ->keyTextInline('idcard_name', '认证名')
//                    ->keyTextInline('auth.idcard_name', '身份证姓名', '实名认证后需要绑定同名支付宝进行提现', '', 'required|chs')
//                    ->keyTextInline('auth.idcard_no', '身份证号', '', '', 'required,identity')
//                    ->keyAuth()
//                    ->keyText('auth.auth_info', '认证信息', '')
//                    ->keyLabel('auth.message', '用户留言')
                ->keyBool('examine', '是否通过认证', '', 1)
//                    ->keyTextInline('refuse', '审核结果', '不通过请写明原因')
//                    ->keyText('remark', '备注', '自动审核未通过结果')
                ->keyTextArea('result', '拒绝原因', '如果拒绝填写')
                ->data($auth)
                ->buttonSubmit('', '提交');
            return $builder->fetch();
        }
    }

}
